﻿$(function () {

    $("#form1").validationEngine({
        promptPosition: "centerRight"
    });

    $("#btnSubmit").click(function () {
        $("#form1").validationEngine({
            promptPosition: "centerRight"
        });

        if ($("#form1").validationEngine('validate') && confirm('是否确定各项信息完全正确并提交注册?')) {
            $('#btnSubmit').attr('readOnly', 'readOnly');
            $("#pageloading").height($(window).height());
            $('#pageloading').show();
            $("#form1").submit();
        }
    });

    loadCardTypes();

    $("#CardClass").change(function () {
        loadCardTypes();
    });

    $("#CardType").change(function () {
        loadJoinMoney();
    });

    $("#JdUserName").blur(function () {
        loadJdSort();
    });

    function loadCardTypes() {
        var cardClass = $("#CardClass").val();
        var ddlCardType = $("#CardType");
        ddlCardType.empty();

        if (cardClass == null || cardClass == '')
            return;

        $.ajax({
            url: '/Users/GetCardTypes',
            type: "GET",
            data: { cardClass: cardClass },
            success: function (data) {
                $.each(data, function (i, item) {
                    $("<option></option>")
                            .val(item["Value"])
                            .text(item["Text"])
                            .appendTo(ddlCardType);
                });
                loadJoinMoney();
            }
        });
    }

    function loadJoinMoney() {
        var cardType = $("#CardType").val();
        if (cardType == null || cardType == '')
            return;
        var joinMoneyHidden = $("#JoinMoney");
        var lblJoinMoney = $("#lblJoinMoney");
        var lblSaleRate = $("#lblSaleRate");

        joinMoneyHidden.val('');
        lblJoinMoney.html('');
        lblSaleRate.html('');

        $.ajax({
            url: "/Users/GetJoinMoney",
            type: 'GET',
            data: { cardType: cardType },
            success: function (data) {
                if (data != null) {
                    joinMoneyHidden.val(data.Price);
                    lblJoinMoney.html(data.Price);
                    lblSaleRate.html(data.SaleRate);
                }
            }
        });
    }

    function loadJdSort() {
        var jdUserName = $("#JdUserName").val();
        var ddlJdSort = $("#JdSort");
        ddlJdSort.empty();

        $.ajax({
            url: '/Users/GetJdSortList',
            type: 'GET',
            data: { userName: jdUserName },
            success: function (data) {
                $.each(data, function (i, item) {
                    $("<option></option>")
                            .val(item["Value"])
                            .text(item["Text"])
                            .appendTo(ddlJdSort);
                });
            }
        });
    }
});

function ExistsUserName() {
    var userName = $("#UserName").val();
    if (userName != null && userName != '') {
        var isValid = false;

        $.ajax({
            url: '/Users/ExistsUser',
            type: "GET",
            async: false,
            data: { userName: userName },
            success: function (data) {
                isValid = data;
            }
        });

        if (!isValid) {
            return "此会员账号已存在";
        }
    }
}

function CanTj() {
    var tjUserName = $("#TjUserName").val();
    if (tjUserName != null && tjUserName != '') {
        var isValid = false;

        $.ajax({
            url: '/Users/CanTj',
            type: "GET",
            async: false,
            data: { userName: tjUserName },
            success: function (data) {
                isValid = data;
            }
        });

        if (!isValid) {
            return "零售商不存在";
        }
    }
}

function CanJd() {
    var jdUserName = $("#JdUserName").val();
    if (jdUserName != null && jdUserName != '') {
        var isValid = false;
        var msg = '';
        $.ajax({
            url: '/Users/CanJd',
            type: "GET",
            async: false,
            data: { userName: jdUserName },
            success: function (data) {
                isValid = data.result;
                msg = data.msg;
            }
        });

        if (!isValid) {
            return msg;
        }
    }
}

function checkRealMoney() {
    var joinMoney = $("#JoinMoney").val();
    var realMoney = $("#RealMoney").val();
    if (parseFloat(realMoney) > parseFloat(joinMoney)) {
        return "[实消金额]不能大于[消费金额]";
    }
}